﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.IO;

namespace TRPOP.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public class Literal : System.Web.UI.WebControls.Literal
    {

    }

    /// <summary>
    /// 
    /// </summary>
    public class LiteralGetFileContent : System.Web.UI.WebControls.Literal
    {
        private string m_strFilePath = "";
        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath
        {
            set { m_strFilePath = value; }
            get { return m_strFilePath; }
        }

        private string m_strPageCoder = "utf-8";
        /// <summary>
        /// 
        /// </summary>
        public string PageCoder
        {
            set { m_strPageCoder = value; }
            get { return m_strPageCoder; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);
            if (m_strFilePath != "")
            {
                string strResult = "";
                try
                {
                    //请求URL
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_strFilePath);
                    //设置超时(10秒)
                    req.Timeout = 10000;
                    //获取Response
                    HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
                    //创建StreamReader与StreamWriter文件流对象
                    StreamReader sr = new StreamReader(rep.GetResponseStream(),Encoding.GetEncoding(m_strPageCoder));
                    strResult = sr.ReadToEnd();
                    sr.Close();
                    sr.Dispose();
                }
                catch
                {
                    strResult = "";
                }
                HttpContext.Current.Response.Write(strResult);
            }
        }
    }
}
